package com.yunbao.hujin.mychoosealbum;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yunbao.hujin.mychoosealbum.utils.Const;
import com.yunbao.hujin.mychoosealbum.utils.ToastUtil;
import com.yunbao.hujin.mychoosealbum.utils.ZLog;

import java.io.File;
import java.util.ArrayList;

/**
 * 适配7.0
 */
public class MainActivity extends AppCompatActivity {

    private final int REQUEST_PICK_IMAGE = 1;
    private final int REQUEST_PICK_IMAGE2 = 2;
    private Uri uriImage;

    private TextView view1, view2;
    private ImageView iv1, iv21, iv22, iv23;
    private ArrayList<ImageView> imgList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if ((ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) ||
                    (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
                //如果没有授权，则请求授权
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
            }
        }

        initView();
    }

    private void initView() {
        view1 = findViewById(R.id.mytext);
        iv1 = findViewById(R.id.iv1);

        //头像选择
        view1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ChooseImgAllActivity.class);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                intent.putExtra(Const.THE_NUM_OF_PHOTO, 1);
                //根据版本号不同使用不同的Action
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(intent, REQUEST_PICK_IMAGE);

                } else {
                    //其实是21
                    intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
                    startActivityForResult(intent, REQUEST_PICK_IMAGE);
                }
            }
        });

        iv21 = findViewById(R.id.iv21);
        iv22 = findViewById(R.id.iv22);
        iv23 = findViewById(R.id.iv23);
        imgList = new ArrayList<>();
        imgList.add(iv21);
        imgList.add(iv22);
        imgList.add(iv23);

        view2 = findViewById(R.id.mytext2);
        view2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ChooseImgAllActivity.class);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                intent.putExtra(Const.THE_NUM_OF_PHOTO, 3);
                startActivityForResult(intent, REQUEST_PICK_IMAGE2);
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 判断结果码,如果为0,表示没有返回结果
        if (resultCode == RESULT_CANCELED) {
            return;
        }
        //头像选择(单张图片)
        if (requestCode == REQUEST_PICK_IMAGE) {
            String selecPath = "";
            //选择照片返回
            if (resultCode == Const.SELECT_RESULT_CODE) {
                ArrayList<String> temp = data.getStringArrayListExtra(Const.SELECT_RESULT_INTENT);//选中照片的地址集合
                if (temp.size() > 0) {
                    selecPath = temp.get(0);
                }

                //拍照选中的照片
            } else if (resultCode == Const.PTO_RESULT_CODE) {
                selecPath = data.getStringExtra(Const.PTO_RESULT_INTENT);
            }
            if (!TextUtils.isEmpty(selecPath)) {
//                uriImage = Uri.fromFile(new File(selecPath));
//                ZLog.e(String.valueOf(uriImage));
//                iv1.setImageURI(uriImage);
                Bitmap bitmap = BitmapFactory.decodeFile(selecPath);
                iv1.setImageBitmap(bitmap);
            }
        }

        //多张图片
        if (requestCode == REQUEST_PICK_IMAGE2) {
            if (data == null)
                return;
            if (resultCode == Const.SELECT_RESULT_CODE) {
                //已选中照片并返回
                ArrayList<String> selectPath = data.getStringArrayListExtra(Const.SELECT_RESULT_INTENT);//选中照片的地址集合
                for (int i = 0; i < selectPath.size(); i++) {
                    Bitmap bitmap = BitmapFactory.decodeFile(selectPath.get(i));
                    imgList.get(i).setImageBitmap(bitmap);
                }

            } else if (resultCode == Const.PTO_RESULT_CODE) {
                //拍照选中的照片---只有一个图片地址
                ArrayList tempPath = new ArrayList();
                String selectPath = data.getStringExtra(Const.PTO_RESULT_INTENT);
                tempPath.add(selectPath);
                if (!TextUtils.isEmpty(selectPath)) {
                    Bitmap bitmap = BitmapFactory.decodeFile(selectPath);
                    iv21.setImageBitmap(bitmap);
                }
            }
        }

    }


}

